package com.jjkj.base.common;

import java.io.Serializable;

/**
 * Rest返回结果
 * @author Rlax
 *
 */
public class R<T> implements Serializable {

    private int code;
    private String message;

    private T data;

    private R() {

    }

    public static R create(int code, String message) {
        return create(code, message, null);
    }

    public static <T> R create(int code, String message, T data) {
        R<T> r = new R<>();
        r.code = code;
        r.message = message;
        r.data = data;
        return r;
    }

    public static R success() {
        return success(null);
    }

    public static <T> R success(T data) {
        return create(ResultCode.SUCCESS, "success", data);
    }

    public static R error() {
        return error("fail");
    }

    public static R error(String msg) {
        return error(ResultCode.ERROR, msg);
    }

    public static R error(int code, String msg) {
        return error(code, msg, null);
    }

    public static <T> R error(String msg, T data) {
        return create(ResultCode.ERROR, msg, data);
    }

    public static <T> R error(int code, String msg, T data) {
        return create(code, msg, data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
